package com.tuanzi.loan.business.repository.wallet.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanUserWallet.LOAN_USER_WALLET;

import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.business.repository.wallet.custom.LoanUserWalletRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.wallet.LoanUserWalletRequest;
import com.tuanzi.loan.vo.response.wallet.LoanUserWalletResponse;
import com.tuanzi.server.loan_base.util.DateUtils;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;

@SuppressWarnings({ "rawtypes" })
@Repository
public class LoanUserWalletRepositoryImpl extends JOOQBaseRepository implements LoanUserWalletRepositoryCustom{
	
	@Override
	protected TableImpl getTable() {
		return LOAN_USER_WALLET;
	}
	
	@Override
	public Page<LoanUserWalletResponse> list(
			LoanUserWalletRequest searchRequest, Pageable pageable) {
		
		fields(LOAN_USER_WALLET.fields());
		eq(LOAN_USER_WALLET.MOBNUM, searchRequest.getMobnum());
		eq(LOAN_USER_WALLET.PRODUCT_ID, searchRequest.getProductId());
		eq(LOAN_USER_WALLET.CHANNEL_CODE, searchRequest.getChannelCode());
		eq(LOAN_USER_WALLET.PLATFORM, searchRequest.getPlatform());
		between(LOAN_USER_WALLET.CREATE_TIME, searchRequest.getStartDate(), 
				searchRequest.getEndDate() == null ? searchRequest.getEndDate() : 
					DateUtils.formatDateToYyyyMMdd(searchRequest.getEndDate()) + " 23:59:59");
		orderBy(LOAN_USER_WALLET.CREATE_TIME, Sort.Direction.DESC);
		
		 return search(pageable, record -> {
			 LoanUserWalletResponse response = new LoanUserWalletResponse();
			 	
	            response.setId(record.get(LOAN_USER_WALLET.ID));
	            response.setCreateTime(record.get(LOAN_USER_WALLET.CREATE_TIME));
	            response.setProductId(record.get(LOAN_USER_WALLET.PRODUCT_ID));
	            response.setChannelCode(record.get(LOAN_USER_WALLET.CHANNEL_CODE));
	            response.setPlatform(record.get(LOAN_USER_WALLET.PLATFORM));
	            
	            String phone = record.get(LOAN_USER_WALLET.MOBNUM);
	            response.setMobnum(PhoneNumNideUtil.hideNum(phone));
	            response.setBalance(record.get(LOAN_USER_WALLET.BALANCE).floatValue());
	            
	            return response;
	        });
	}

	


}
